import networkx as nx
import matplotlib.pyplot as plt


def draw_graph(n, edges, directed=True, weighted=False):
    # 创建一个有向图对象
    G = nx.DiGraph() if directed else nx.Graph()
    # 添加节点
    G.add_nodes_from([i for i in range(n)])
    # 添加边
    if weighted:
        G.add_weighted_edges_from([(u, v, w) for u, v, w in edges])
    else:
        G.add_edges_from([(u, v) for u, v in edges])
    # 绘制图形
    pos = nx.spring_layout(G)  # 定义节点布局
    nx.draw_networkx_nodes(G, pos, node_size=200, node_color='lightblue')  # 绘制节点
    nx.draw_networkx_edges(G, pos, edge_color='gray', width=2)  # 绘制边
    nx.draw_networkx_labels(G, pos, font_size=12, font_color='black')  # 绘制节点标签
    # 显示图形
    plt.axis('off')
    plt.show()


if __name__ == '__main__':
    n = 13
    edges = [[12,8],[2,4],[3,7],[6,8],[11,8],[9,4],[9,7],[12,4],[11,4],[6,4],[1,4],[10,7],[10,4],[1,7],[1,8],[2,7],[8,4],[10,8],[12,7],[5,4],[3,4],[11,7],[7,4],[13,4],[9,8],[13,8]]
    draw_graph(n, edges)